<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a
        href="javascript:;"
        @click="hasAccount = 'isBind'"
        :class="{active:hasAccount === 'isBind'}"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        href="javascript:;"
         @click="hasAccount = 'isPatch'"
         :class="{active:hasAccount === 'isPatch'}"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
  <div  v-if="hasAccount === 'isBind'">
      <CallbackBind :union-id="unionId"/>
  </div>
  <div v-if="hasAccount === 'isPatch'">
      <CallbackPatch :union-id="unionId"/>
  </div>
  </section>
  <LoginFooter />
</template>

<script>

import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { onMounted, ref } from 'vue'
import { userQQLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import QC from 'qc'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    const hasAccount = ref('isBind')
    const store = useStore()
    const router = useRouter()
    const unionId = ref()
    onMounted(() => {
      // 1.检测是否已经进行qq登录 （扫码或者是输入qq账号密码）
      // 2.使用qq提供的api获取 openId
      // 3.使用openId作为参数调用我们自己的接口检测当前qq是否已经OK
      if (QC.Login.check()) {
        QC.Login.getMe((openId) => {
          unionId.value = openId
          userQQLogin(openId).then(res => {
            // 当前qq号已绑定的账号
            console.log(res)
            // 保存数据到vuex中
            store.commit('user/setUser', res)
            // 路由跳转
            router.push('/')
          }).catch(error => {
            console.log(error)
          })
        })
      }
    })
    return {
      hasAccount,
      unionId
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
